home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SKY.ZIP / README.TXT < prev    next >
Text File  |  1996-02-14  |  5KB  |  125 lines

  1. This archive contains some example code to draw a sky plane, as
  2. seen in Tim Clarke's MARS demo.
  3.  
  4. The code is for Turbo Pascal and MASM, but should be easily portable
  5. to C/TASM since all the important routines are in assembly language.
  6. (I don't have a real mode C compiler, so I couldn't write a C wrapper).
  7.  
  8. This is essentially a constant-z texture-mapping problem, but since 
  9. the sky plane is only in one orientation a number of optimisations 
  10. can be made, making the final routine quite fast (under 5 cycles/pixel 
  11. on a 486). This routine also uses 2x2 dithering on the sky map, which 
  12. makes it look _much_ better :). A similar technique can be used to 
  13. dither the ground in Tim Clarke's algorithm, which again reduces banding 
  14. noticeably.
  15.  
  16. The idea for the dithering algorithm was obtained from Chris Green, in
  17. a post on rec.games.programmer:
  18.  
  19. Path: newsroom.utas.edu.au!munnari.oz.au!network.ucsd.edu!usc!cs.utexas.edu!uunet!olivea!gossip.pyramid.com!pyramid!cbmvax!chrisg
  20. From: chrisg@cbmvax.cbm.commodore.com (Chris Green)
  21. Newsgroups: rec.games.programmer
  22. Subject: Re: Help w/Gouraud Shading
  23. Message-ID: <C8tGzC.8Dz@cbmvax.cbm.commodore.com>
  24. Date: 18 Jun 93 12:21:11 GMT
  25. References: <1vlao2$nsg@msuinfo.cl.msu.edu> <1993Jun16.160757.8197@pony.Ingres.COM>
  26. Reply-To: chrisg@cbmvax.cbm.commodore.com (Chris Green)
  27. Organization: Commodore, West Chester, PA
  28. Lines: 86
  29.  
  30. In article <1993Jun16.160757.8197@pony.Ingres.COM> edg@Ingres.COM (Ed Goldman) writes:
  31. >klein@egr.msu.edu (Jeffrey Klein) writes:
  32. >: In article <1993Jun15.170045.8377@pony.Ingres.COM> edg@Ingres.COM (Ed Goldman) writes:
  33. >: 
  34. >: Calculate the color with higher precision than you can display, then
  35. >: use the remainder to dither between the two closest.
  36. >: 
  37. >
  38. >Thanks to everyone who responded!  As it turns out, the above was the correct
  39. >answer to what I wanted to do.  I realized that I already had the color
  40. >in a higher precision in a temp fixedpoint color variable, and it was
  41. >just a matter of adding a few lines of code to dither.  Looks great now!
  42.  
  43.  
  44.     If you have to add ANY lines of code to the loop to dither, then
  45. you're wasting time:
  46.  
  47.     mov al,bh       ; output color(even)
  48.     add bx,bp       ; color(even)+=delta
  49.     mov ah,dh       ; output color(odd)
  50.     add dx,bp       ; color(odd)+=delta
  51.     stosw           ; and store two pixels
  52.  
  53.     First compute startcolor, and deltacolor as normal for
  54. gouraud shading. Then, conditionally take a one-pixel step
  55. in order to get the destination pointer word-aligned.
  56.  
  57.     Now you want to change the iteration variables to
  58. handle even and odd separately. set color(odd)=startcolor+delta+0.5.
  59. set deltacolor=deltacolor*2 and go (and of course handle the case
  60. when there is an odd number of pixels to render). What you
  61. end up with is a +.5 bias on odd pixels. So, if you were rendering
  62. a long run of pixels of color value $0180 (1.5), you
  63. will end up with outputs of 1,2,1,2,1,2.. for a perfect 2x2 dither.
  64.  
  65.     To do the vertical dither, reverse the sense of odd and
  66. even pixels based upon the low bit of (startx xor ycoord).
  67.  
  68.     The same effect can be achieved by using two deltas and
  69. one iterator as well. The net result is the same: 2x2 dither
  70. "for free".
  71.  
  72.     Cool gouraud trick #2:
  73.  
  74.     instead of using (for instance) linear ranges in the palette
  75. (colors 0-15 for gray, for instance), use scattered ranges.
  76. If you want 16-color ranges, use color 00, 10, 20, 30,... f0 for 16 grays.
  77. Then, you can generate _4_ pixels with the following 386 code:
  78.  
  79.     add eax,ebp             ; colors += deltas
  80.     mov ebx,eax             ; copy colors
  81.     and ebx,edx             ; mask out lower values
  82.     or  ebx,esi             ; and OR in range selector
  83.     mov [edi],ebx   ; 4 pixels!
  84.  
  85.     In our example, we would set edx (mask) = f0f0f0f0.
  86. esi would be zero. The deltas are packed in 4.4 format into
  87. ebp.
  88.  
  89.     To handle negative deltas, you run the loop in the opposite
  90. direction. Only four bits of fraction is kinda small, but it works
  91. in many cases. Extra precision can be gained back for small deltas
  92. by using one aditional integrator which adds 0x01010101 to
  93. the color variable when it overflows:
  94.  
  95.     dec     cl                      ; count down whole numbers
  96.     jnz     no_inc_color
  97.     add     eax,0x01010101
  98.     mov cl,clsave
  99. no_inc_color:
  100.     add     eax,ebp         ; colors += deltas
  101.     mov ebx,eax             ; copy colors
  102.     and ebx,edx             ; mask out lower values
  103.     or  ebx,esi             ; and OR in range selector
  104.     mov [edi],ebx   ; 4 pixels!
  105.     
  106.     this routine is only used when the magnitude of deltacolor/dx is
  107. small.
  108. -- 
  109. *-------------------------------------------*---------------------------*
  110. |Chris Green - Graphics Software Engineer   - chrisg@commodore.COM      f
  111. |                  Commodore-Amiga          - rutgers!cbmvax!chrisg     n
  112. |My opinions are my own, and do not         - icantforgettheimpression  o
  113. |necessarily represent those of my employer.- youmadeyouleftaholeinthe  r
  114. |"A screaming comes across the sky..."      - backofmyhead              d
  115. *-------------------------------------------*---------------------------*
  116.  
  117.  
  118. Well, what can you say? Thanks, Chris.
  119.  
  120. I hope you find this code useful. If you see any glaring errors or 
  121. optimisations I missed (TANSTATFC), please let me know...
  122.  
  123. Mark Mackey.
  124. mdm1004@cus.cam.ac.uk (I'll be here until December 1996 at least).
  125.